#coding=utf-8
#jzx
import requests
from bs4 import BeautifulSoup
import os
from urllib.parse import urlparse
from openai import OpenAI
import re

class CSDNBlogReader:
    def __init__(self, openai_api_key):
        self.client = OpenAI(api_key=openai_api_key, base_url="https://api.deepseek.com")
        
    def get_blog_content(self, url):
        """获取CSDN博客内容"""
        try:
            headers = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
            }
            response = requests.get(url, headers=headers)
            response.encoding = 'utf-8'
            soup = BeautifulSoup(response.text, 'html.parser')
            # 获取文章标题
            title = soup.find('h1', class_='title-article').text.strip()
            # 获取文章内容
            content_div = soup.find('div', id='article_content')
            if content_div:
                # 移除所有script标签
                for script in content_div.find_all('script'):
                    script.decompose()
                content = content_div.get_text(separator='\n', strip=True)
                return title, content
            return None, None
        except Exception as e:
            print(f"获取博客内容时出错: {str(e)}")
            return None, None

    def save_to_markdown(self, title, summary, output_dir="summaries"):
        """将总结保存为Markdown文件"""
        # 创建输出目录
        if not os.path.exists(output_dir):
            os.makedirs(output_dir)
        # 清理文件名（移除非法字符）
        safe_title = re.sub(r'[\\/*?:"<>|]', "", title)
        filename = f"{safe_title}.md"
        filepath = os.path.join(output_dir, filename)
        # 写入Markdown文件
        with open(filepath, 'w', encoding='utf-8') as f:
            f.write(f"# {title}\n\n")
            f.write(summary)
        print(f"总结已保存到: {filepath}")
        return filepath

    def summarize_content(self, title, content):
        """使用OpenAI API总结内容"""
        try:
            prompt = f"""请用通俗易懂的语言总结以下技术博客文章：

标题：{title}

内容：
{content}

请按照以下格式输出：
1. 文章主要讲了什么
2. 关键知识点
3. 迁移到电力场景的可行性
4. 学习建议

既要保持学术严谨性，又要通俗易懂。"""
            
            response = self.client.chat.completions.create(
                model="deepseek-chat",
                messages=[
                    {"role": "system", "content": "你是一个专业的技术文章总结助手，擅长在保持学术严谨性的同时，用通俗易懂的语言解释技术概念。"},
                    {"role": "user", "content": prompt}
                ],
                stream=False
            )
            
            summary = response.choices[0].message.content
            # 保存为Markdown文件
            self.save_to_markdown(title, summary)
            return summary
        except Exception as e:
            print(f"总结内容时出错: {str(e)}")
            return None

def main():
    # 设置OpenAI API密钥
    openai_api_key = "******"

    reader = CSDNBlogReader(openai_api_key)
    
    # 预定义的CSDN博客链接
    blog_urls = [
        "https://blog.csdn.net/v_JULY_v/article/details/144176405",  # 示例链接1
        "https://blog.csdn.net/v_JULY_v/article/details/143472442",
         "https://blog.csdn.net/v_JULY_v/article/details/143180794"
    ]
    
    for url in blog_urls:
        print(f"\n正在处理博客链接: {url}")
        if not urlparse(url).netloc.endswith('csdn.net'):
            print("无效的CSDN博客链接")
            continue
            
        title, content = reader.get_blog_content(url)
        if title and content:
            print("\n正在分析文章...")
            summary = reader.summarize_content(title, content)
            if summary:
                print("\n文章总结：")
                print(summary)
            else:
                print("总结失败")
        else:
            print("获取博客内容失败")

if __name__ == "__main__":
    main()
